﻿Imports System.Collections
Imports System.Collections.Generic
Imports System.Security.Cryptography
Imports System.IO
Imports System.Configuration
Imports System.Text

Namespace helper
    ''' Author : Aswad32
    ''' Date Created : 21/08/2008
    ''' Last Modified : -
    ''' This is Implemetation for Encryption and Decryption
    ''' See : application configuration (cyptokey) for key setting
    ''' for configuration key

    Public NotInheritable Class SecurityHelper
        ' Get the string cryptokey from application config file
        Private strCryptoKey As String = "x12al3nierkjr"
        Private IV() As Byte = {50, 199, 10, 159, 132, 55, 236, 189, _
                                51, 243, 244, 91, 17, 136, 39, 230}

        Private Shared Function GetHash(ByVal plainText As String) As String
            Dim plainBytes As Byte()
            Dim hashEngine As MD5CryptoServiceProvider
            Dim hashBytes As Byte()
            Dim hashText As String

            plainBytes = Encoding.UTF8.GetBytes(plainText)

            hashEngine = New MD5CryptoServiceProvider
            hashBytes = hashEngine.ComputeHash(plainBytes)
            hashText = Replace(BitConverter.ToString(hashBytes), "-", "")
            Return hashText
        End Function

        ''' <summary>
        ''' encrypting text
        ''' </summary>
        ''' <param name="plainText"></param>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Shared Function Encrypt(ByVal plainText As String) As String
            Dim crypto As New SecurityHelper
            Dim workText As String = plainText.Replace(vbNullChar, "")

            Dim strBytes() As Byte = Encoding.UTF8.GetBytes(workText)
            Dim strKeyBytes() As Byte = Encoding.UTF8.GetBytes(crypto.GetHash(crypto.strCryptoKey))

            Dim rijndael As New RijndaelManaged
            Dim memoryStream As New MemoryStream

            Dim cryptoTransform As ICryptoTransform

            cryptoTransform = rijndael.CreateEncryptor(strKeyBytes, crypto.IV)

            Dim cryptoStream As New CryptoStream( _
            memoryStream, cryptoTransform, CryptoStreamMode.Write)

            cryptoStream.Write(strBytes, 0, strBytes.Length)
            cryptoStream.FlushFinalBlock()

            Dim encrypted As String = _
                Convert.ToBase64String(memoryStream.ToArray)

            memoryStream.Close()
            cryptoStream.Close()
            crypto = Nothing
            Return encrypted
        End Function

        ''' <summary>
        ''' shared function for decrypting text
        ''' </summary>
        ''' <param name="strEncrypted"></param>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Shared Function Decrypted(ByVal strEncrypted As String) As String
            Dim crypto As New SecurityHelper

            Dim workBytes() As Byte = _
                Convert.FromBase64String(strEncrypted)
            Dim keyBytes() As Byte = _
                Encoding.UTF8.GetBytes(crypto.GetHash(crypto.strCryptoKey))

            Dim tempBytes(workBytes.Length - 1) As Byte

            Dim rijndael As New RijndaelManaged

            Dim memoryStream As New MemoryStream(workBytes)

            Dim cryptoTransform As ICryptoTransform
            cryptoTransform = _
                rijndael.CreateDecryptor(keyBytes, crypto.IV)

            Dim cryptoStream As New CryptoStream(memoryStream, _
                                                  cryptoTransform, CryptoStreamMode.Read)

            cryptoStream.Read(tempBytes, 0, tempBytes.Length)

            'clear all the stream being used
            memoryStream.Close()
            cryptoStream.Close()
            crypto = Nothing

            Dim plainText As String = _
                Encoding.UTF8.GetString(tempBytes)
            Return plainText.Replace(vbNullChar, "")
        End Function


    End Class

End Namespace
